// pages/book/book.js
import config from "../../../config/config"
import getData from "./data";
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		bookObj: null,
		pageNumber: 1,
		sys: null,
		// progress: 0,
		// headerSrc: "http://192.168.1.52:8080/lala.png",
		isAutoPlay: true, //是佛自动播放
		isMute: false, //是否静音
		isSuspend: false, //是否暂停
		show: false,
		list: [],
		timer: null,
		// ZiMu: "1121212",
		// isPull: false, //废弃 是否正在拖动
		isShowCatalog: false,
		unlock: 3,
		imgUrl: config.imgUrl
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let book = this.selectComponent("#book")
		let sys = wx.getSystemInfoSync()
		this.data.sys = sys
		let id = options.id ? options.id : 1
		console.log(id)
		this.setData({
			list: getData(id)
		})
		this.data.bookObj = book
		book.whFn()
	},
	catalogFn() {
		this.setData({
			isShowCatalog: !this.data.isShowCatalog
		})
		if (this.data.isShowCatalog == false) {
			this.setData({
				show: false
			})
		}
		// this.showFn()
	},
	beginFn() {
		this.setData({
			isSuspend: !this.data.isSuspend
		})
		if (this.data.isSuspend) {
			this.data.bookObj.stopPlay()
			this.data.bookObj.pause()
		} else {
			if (this.data.isAutoPlay) {
				this.data.bookObj.AutoPlay()
			}
			if (!this.data.isMute) {
				this.data.bookObj.playAudio()
			}
		}
	},
	backFn() {
		wx.navigateBack({
			delta: 1,
		})
	},
	showFn() {
		if (this.data.show && this.data.isShowCatalog) {
			return
		}
		this.setData({
			show: !this.data.show
		})
		if (this.data.show) {
			clearTimeout(this.data.timer)
			this.data.timer = setTimeout(() => {
				if (!this.data.isShowCatalog) {
					this.setData({
						show: !this.data.show
					})
				}

			}, 4000)
		}
	},
	// 字幕改变时触发
	ZiMuChange(e) {
		// console.log(e)
		// this.setData({
		// 	ZiMu: e.detail.text,
		// 	headerSrc: e.detail.role
		// })
	},
	skipBook(e) {
		this.data.bookObj.skip(e.currentTarget.dataset.index)
	},


	outoPlay() {

		this.setData({
			isAutoPlay: !this.data.isAutoPlay
		})
		if (this.data.isAutoPlay) {
			this.data.bookObj.AutoPlay()
		} else {
			this.data.bookObj.stopPlay()
		}
	},
	muteFn() {
		this.setData({
			isMute: !this.data.isMute
		})
		if (this.data.isMute) {
			this.data.bookObj.MuteFn()
		} else {
			this.data.bookObj.MuteFnOff()
		}
	},
	astrictFn() {
		wx.navigateTo({
			url: '/pages/vipInfo/vipInfo',
		})
		this.data.bookObj.stopPlay()
	},
	bookChange(e) {
		this.setData({
			pageNumber: e.detail.pageIndex + 1,
			isAutoPlay: e.detail.isAutoPlay
		})
	},
	bookEnd() {
		wx.redirectTo({
			url: '/pages/topic/topic',
		})
	},



	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		if (this.data.bookObj && !this.data.isMute) {
			this.data.bookObj.MuteFnOff()
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {
		console.log("看书被隐藏")
		this.data.bookObj.MuteFn()
		this.data.bookObj.stopPlay()
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		console.log("看书被卸载")
		this.data.bookObj.MuteFn()
		this.data.bookObj.stopPlay()
		this.data.bookObj.destroy()
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})